Note: This tutorial assumes that you have completed the previous tutorials: Installing ROS on your Ubuntu system.
(!) Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

Cross-Compiling ROS for the Raspberry Pi

Description: Instructions how to cross-compile ROS bare-bones for the Raspberry Pi on an Ubuntu system

Tutorial Level: INTERMEDIATE

Install a cross-compiler toolchain

Before you can cross-compile ros you need to install a toolchain capable of producing executables for the pi

cd /opt
sudo git clone https://github.com/raspberrypi/tools.git raspberrypi-tools

In order for the linker to find the pi's libraries you have to mount some of the pi's directories on your development pc

sudo mkdir /lib/arm-linux-gnueabihf
sudo chown <your userid>:<your groupid> /lib/arm-linux-gnueabihf/
sshfs <pi userid>@<pi ip-address>:/lib/arm-linux-gnueabihf /lib/arm-linux-gnueabihf/
sudo mkdir /usr/lib/arm-linux-gnueabihf
sudo chown <your userid>:<your groupid> /usr/lib/arm-linux-gnueabihf/
sshfs <pi userid>@<pi ip-address>:/usr/lib/arm-linux-gnueabihf /usr/lib/arm-linux-gnueabihf/
mkdir -p ~/mnt/pi
sshfs <pi userid>@<pi ip-address>:/ ~/mnt/pi

Create and initialize a workspace

mkdir ~/Projects/ros-pi/ros_catkin_barebones_ws
cd ~/Projects/ros-pi/ros_catkin_barebones_ws
rosinstall_generator ros_comm --rosdistro melodic --deps --tar > melodic-ros_comm.rosinstall
wstool init -j8 src melodic-ros_comm.rosinstall

Create a toolchain configuration file

vi ~/Projects/ros-pi/toolchain.cmake

and make this it's contents

set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_VERSION 1)
set(CMAKE_LIBRARY_ARCHITECTURE arm-linux-gnueabihf)

set( CMAKE_C_COMPILER   "/opt/pi-tools/arm-bcm2708/arm-rpi-4.9.3-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc")
set( CMAKE_CXX_COMPILER "/opt/pi-tools/arm-bcm2708/arm-rpi-4.9.3-linux-gnueabihf/bin/arm-linux-gnueabihf-g++")
#set( CMAKE_C_COMPILER   "/opt/pi-tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf-gcc")
#set( CMAKE_CXX_COMPILER "/opt/pi-tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf-g++")

#set( CMAKE_C_COMPILER   "/usr/bin/arm-linux-gnueabihf-gcc")
#set( CMAKE_CXX_COMPILER "/usr/bin/arm-linux-gnueabihf-g++")

#set(CMAKE_FIND_ROOT_PATH "/opt/pi-tools/arm-bcm2708/arm-rpi-4.9.3-linux-gnueabihf/arm-linux-gnueabihf/sysroot/")
set(CMAKE_FIND_ROOT_PATH "/home/erik/mnt/pi")

#set(CMAKE_CXX_FLAGS "-Wl,-rpath-link,/home/erik/mnt/pi/lib/arm-linux/gnueabihf" CACHE STRING "" FORCE)

set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

include_directories(/home/erik/mnt/pi/usr/include/arm-linux-gnueabihf/)

Patch the cmake configuration files

In order to successfully configure and compile ros_comm you we need to patch a number of CMakeLists.txt files

First download the patch from github and extract it in ~/Projects/ros-pi/

Now apply the patch by issueing:

cd ~/Projects/ros-pi/ros_catkin_barebones_ws
patch -p1 < ../ros_melodic_pi.patch

Cross-compile ros_comm

Finally we can start cross-compiling, issue:

cd ~/Projects/ros-pi/ros_catkin_barebones_ws
./src/catkin/bin/catkin_make_isolated --install -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_TOOLCHAIN_FILE=/home/erik/Projects/ros-pi/toolchain.cmake

If cmake cannot find any dependencies (e.g. required libraries) you will have to install the libraries -dev package using su apt-get install <library>-dev on the Raspberry Pi and re-execute the above command.

When compilation finishes the directory ~/Projects/ros-pi/ros_catkin_barebones_ws/install_isolated will contain the cross-compiled ros_comm system. You will have to copy that directory over to the pi eg:

scp -r ~Projects/ros-pi/ros_catkin_barebones_ws pi@<ip-address>/opt/ros/melodic

Then before doing anything ros related on the pi you have to setup the ros environment by issuing:

source /opt/ros/melodic/setup.bash

Wiki: ROS/CrossCompiling/RaspberryPi/Cross-Compile ROS for the RaspberryPi (last edited 2019-09-23 23:40:58 by GuidoSanchez)